package com.eight.group.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.eight.group.entity.PageResult;
import com.eight.group.entity.QueryPageBean;
import com.eight.group.entity.Result;
import com.eight.group.pojo.Permission;
import com.xqx.eight.group.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//权限管理
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    //查询权限管理(t_permissions)
    @ResponseBody
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return permissionService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }

    //新增权限
    @ResponseBody
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败");
        }
        return new Result(true,"新增权限成功");
    }

    //查询所有权限
    @ResponseBody
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findAll(){
        try {
            return new Result(true,"查询权限成功",permissionService.findAll());
        } catch (Exception e) {
            return new Result(false, "查询权限失败");
        }
    }

    @RequestMapping("/edit")
    @ResponseBody
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true, "编辑权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑权限失败");
        }
    }


    @RequestMapping("/findById")
    @ResponseBody
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"查询权限成功",permission);
        } catch (Exception e) {
            return  new Result(false, "查询权限失败");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }



}
